function M_prepareCSD(varargin)
% This file is part of MANTA licensed under the GPL. See MANTA.m for details.

P = parsePairs(varargin);
checkField(P,'Animal','Danube');
checkField(P,'Penetration',1);
checkField(P,'Electrodes',[1:32]);
checkField(P,'DB',0);
checkField(P,'Reload',1);

% GET LIST OF DEPTHS FOR ANALYSIS
RecList = LF_returnCSDDepths(P.Animal,P.Penetration);
for i=1:length(RecList)
  Penetrations{i} = RecList{i}{1};
  Depths{i} = RecList{i}{2};
  Recordings{i} = RecList{i}{3};
  RealDepths{i} = RecList{i}{4};
end

for iD=1:length(RecList)
  fprintf(['Processing Depth ',n2s(iD),' ... \n']);
  if iD<7 P.Reload = 0; else P.Reload = 1; end
  % LOAD FILES FOR ALL DEPTHS
  Files = MD_collectRecordingsByPen('Animal',P.Animal,'Penetration',Penetrations{iD},'DB',P.DB,'Reload',1);
  Ind = structSelect(Files,'V1 & V2 & V3 & V4',...
    'Recording',Recordings{iD},'Trial',1,'Electrode',1,'Depth',Depths{iD});
  for i=1:length(P.Electrodes)
    cR = evpread(Files(Ind).FileName,'lfpchans',P.Electrodes(i),...
      'spikechans',[],'auxchans',[],'rawchans',[],'trials',inf,'wrap',1);
    cLFP(:,i,:) = cR.LFP;
  end
  E = MD_getElectrodeGeometry('Animal',P.Animal,'Penetration',Penetrations{iD},...
    'Depth',Depths{iD},'Recording',Recordings{iD});
  Channel2Electrode = [E.Electrode];
  D(1,iD).LFP = squeeze(mean(cLFP,3));
  D(1,iD).LFP(:,Channel2Electrode) = D(1,iD).LFP;
  ChannelXY = reshape([E.ChannelXY],2,length(E))';
  D(1,iD).ChannelXY(Channel2Electrode,:) = ChannelXY;
  D(1,iD).Depth = RealDepths{iD};
  clear cLFP;
end

for iE=P.Electrodes
  LFP(iE).ChannelXY =D(1).ChannelXY(iE,:);
  for  iD = 1:length(D)
    LFP(iE).LFP(iD,1:size(D(iD).LFP,1)) = D(iD).LFP(:,iE)';
    LFP(iE).Depths(iD) = D(iD).Depth;
  end
end


% SAVE FOR CSDplotter
Sep = HF_getSep;
if strcmp('PCWIN',computer) Base = ['D:\']; else Base = '/media/storage/'; end
OutFile = [Base,'Results',Sep,P.Animal,Sep,'LFPforCSD.mat'];
fprintf(['Saving to ',OutFile,'\n']);
save(OutFile,'LFP');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function FileList = HF_createFileList(Animal,Penetration,Depths,Cells,Cond,Trial,ENum)

Dirs = setgetDirs; FPath = Dirs.Ferrets; Sep = HF_getSep;

BasePath = [FPath,Animal,Sep,Penetration,Sep,'raw',Sep];
Files = dir([BasePath,'*.evp']);
FileNames = {Files.name};
RE = ['(?<Animal>[a-zA-Z]*)(?<Penetration>[0-9]*)(?<Depth>[a-z])'...
  '(?<Cell>[0-9]{2,3})(?<Cond>[a-zA-Z_]+)\.(?<Trial>[0-9]{3,5})\.(?<ENum>[0-9]*).evp'];
Matches = regexp(FileNames,RE,'names'); Matches = [Matches{:}];
for i=1:length(Depths)
  cDI = find(strcmp(Depths{i},{Matches.Depth}));
  for j=1:length(cDI)
    if Cells{i}==str2num(Matches(cDI(j)).Cell) &...
        strcmp(Cond,Matches(cDI(j)).Cond) &...
        Trial==str2num(Matches(cDI(j)).Trial) & ...
        ENum==str2num(Matches(cDI(j)).ENum)
      FileList{i} = [BasePath,FileNames{cDI(j)}];
    end
  end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function List = LF_returnCSDDepths(Animal,Penetration)

switch Animal
  case 'Lime';
    switch Penetration
      case 1;
        List = {...
          {1,'a',1,75},...
          {1,'b',1,105},...
          {1,'c',3,135},...
          {1,'d',1,200},...
          {1,'e',1,240},...
          {1,'f',1,280},...
          {1,'g',1,320},...
          {1,'h',1,360},...
          {1,'i',1,400},...
          {1,'j',3,440},...
          {1,'k',1,480},...
          };
      case 2;
        List = {...
          {2,'a',1,320},...
          {2,'b',1,360},...
          {2,'c',1,400},...
          {2,'d',1,440},...
          {2,'e',1,480},...
          {2,'f',1,520},...
          {2,'g',1,560},...
          {2,'h',1,600},...
          {2,'i',1,640},...
          {2,'j',1,680},...
          {2,'k',1,720},...
          };
    end
  case 'Danube';
    ListNoise = {
      {3,'a',3,0},...
      {18,'a',9,0},...
      {19,'a',2,0},...
      {20,'a',2,0},...
      {21,'a',2,0},...
      {22,'a',2,0},...
      {23,'a',2,0},...
      {24,'a',2,0},...
      {25,'a',3,0},...
      {26,'a',2,0},...
      {27,'a',3,0},...
      {28,'a',2,0},...
      {29,'a',2,0},...
      {30,'a',2,0},...
      {31,'a',3,0},...
      {32,'a',3,0},...
      {33,'a',4,0},...
      {34,'a',3,0},...
      {35,'a',4,0},...
      {36,'a',3,0},...
      {37,'a',3,0},...
      {38,'a',3,0},...
      {39,'a',3,0},...
      {40,'a',4,0},...
    };
    ListFTC = {
      {3,'a',1,2380},...
      {4,'a',1,2450},...
      {5,'a',1,2520},...
      {6,'a',1,2590},...
      {7,'a',5,2730},...
      {8,'a',2,3430},... %{9,'a',1,3500},... % 
      {10,'a',2,3570},...
      {11,'a',2,3640},...
      {12,'a',2,3710},...
      {13,'a',4,3780},...
      {14,'a',2,3850},...
      {15,'a',2,3920},...
      {16,'a',2,3990},...
      {17,'a',2,4060},...
      {18,'a',2,4130},...
      {19,'a',3,4130},...
      {20,'a',3,4200},...
      {21,'a',3,4270},...
      {22,'a',3,4340},...
      {23,'a',3,4410},...
      {24,'a',3,4480},...
      {25,'a',7,4550},...
      {26,'a',3,4620},...
      {27,'a',4,4760},...
      {28,'a',3,4900},...
      {29,'a',3,4970},...
      {30,'a',3,5180},...
      {31,'a',2,5320},...
      {32,'a',2,5460},...
      {33,'a',3,5600},...
      {34,'a',2,5600},...
      {35,'a',3,5600},...
      {36,'a',2,5600},...
      {37,'a',2,5600},...
      {38,'a',2,5600},...
      {39,'a',2,5600},...
      {40,'a',3,5600},...
      {43,'a',1,5600},...
      {45,'a',2,5600},...
      {46,'a',4,5600},...
      {47,'a',5,5600},...
      {48,'a',6,5600},...
      {49,'a',7,5600},...
      {50,'a',6,5600},...
      
      
      
      
    };
  List = ListFTC;
    
  otherwise error('Animal not documented yet.');
end

